<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学习名称" prop="studyName">
        <el-input
          v-model="queryParams.studyName"
          placeholder="请输入学习名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['api:studyCenter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['api:studyCenter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:studyCenter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['api:studyCenter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studyCenterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="studyId" />-->
      <el-table-column label="标签分类" align="center" prop="studyExtra1" />
      <el-table-column label="学习名称" align="center" prop="studyName" />
<!--      <el-table-column label="学习描述" align="center" prop="studyDetail" />-->
      <el-table-column label="学习关键字" align="center" prop="studyKey" />
      <el-table-column label="学习图片" align="center" prop="studyImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.studyImage" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="备用字段2" align="center" prop="studyExtra2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:studyCenter:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:studyCenter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学习中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签" prop="studyExtra1">
          <el-select v-model="form.studyExtra1" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.catalogId"
              :label="item.catalogName"
              :value="item.catalogId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="studyName">
          <el-input v-model="form.studyName" placeholder="请输入学习名称" />
        </el-form-item>
        <el-form-item label="关键词" prop="studyKey">
          <el-input v-model="form.studyKey" placeholder="请输入学习关键字" />
        </el-form-item>
        <el-form-item label="图片" prop="studyImage">
          <image-upload v-model="form.studyImage"/>
        </el-form-item>
        <el-form-item label="描述" prop="studyDetail">
          <editor v-model="form.studyDetail" :min-height="192"/>
        </el-form-item>

         <div>
           <div style="padding: 10px 0;font-size: 16px">简介:</div>
           <div>
             <div>
               <span>标题:</span>
               <span style="margin-left: 20px"><input v-model="centerDetailListitem.detailSmallTitle" style="width: 60%" type="text"></span>
             </div>
               <div>
                 <span style="padding: 10px 0;display: block">内容:</span>
                 <span>
                     <editor v-model="centerDetailListitem.detailContent" :min-height="192"/>
                 </span>
               </div>
           </div>
         </div>
        <div>
          <div style="margin-top: 20px;font-size: 16px">功能</div>
          <div v-for="(item,index) in centerDetailList" :key="index">
            <div>
              <div>
                <span>标题:</span>
                <span style="margin-left: 20px"><input v-model="item.detailSmallTitle" style="width: 60%" type="text"></span>
              </div>
              <div>
                <span style="padding: 10px 0;display: block">内容:</span>
                <span>
                     <editor v-model="item.detailContent" :min-height="192"/>
                 </span>
              </div>
              <div style="margin: 10px 0" @click="deletefn(index)">  <el-button type="danger">删除</el-button></div>
            </div>
          </div>
          <div @click="additem" style="margin-top: 20px">
            <el-button type="primary">添加</el-button>
          </div>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudyCenter, getStudyCenter, delStudyCenter, addStudyCenter, updateStudyCenter } from "@/api/course/studyCenter";
import {listProductCatalog} from "@/api/course/productCatalog"
export default {
  name: "StudyCenter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习中心表格数据
      studyCenterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studyName: null,
        studyDetail: null,
        studyKey: null,
        studyImage: null,
        studyExtra1: null,
        studyExtra2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      options:[],
      centerDetailList:[],
      centerDetailListitem:{
        detailBigTitle:'1',
        detailSmallTitle:'',
        detailContent:''
      }

    };
  },
  created() {
    this.getstaduyopyion()

    // this.getList();
  },
  methods: {
    additem(){
      this.centerDetailList.push({
        detailBigTitle:'2',
        detailSmallTitle:'',
        detailContent:''
      })
    },
    deletefn(idx){
      this.centerDetailList.splice(idx,1)
    },
    //获取前学习中心类型
    getstaduyopyion(){
      listProductCatalog().then(res=>{
        console.log(res)
        this.options=res.rows[1].children
        this.getList();
      })
    },
    /** 查询学习中心列表 */
    getList() {
      this.loading = true;
      listStudyCenter(this.queryParams).then(response => {
        this.studyCenterList = response.rows;
        let arr = this.studyCenterList;
        arr.forEach(item => {
          item.studyExtra1 = this.getValue(this.options, item.studyExtra1);

        });
        this.studyCenterList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item == arr[i].catalogId) {
          return arr[i].catalogName
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studyId: null,
        studyName: null,
        studyDetail: null,
        studyKey: null,
        studyImage: null,
        studyExtra1: null,
        studyExtra2: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.centerDetailListitem={
        detailBigTitle:'1',
        detailSmallTitle:'',
        detailContent:''
      };
      this.centerDetailList=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学习中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studyId = row.studyId || this.ids
      getStudyCenter(studyId).then(response => {
        this.form = response.data;
        this.form.studyExtra1=this.getValue(this.options, this.form.studyExtra1)
       this.centerDetailList=this.form.functionalList
        this.centerDetailListitem=this.form.introList[0]

        this.open = true;
        this.title = "修改学习中心";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studyId != null) {
            this.centerDetailList.push(this.centerDetailListitem)
            this.form.centerDetailList=this.centerDetailList
            updateStudyCenter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.centerDetailList.push(this.centerDetailListitem)
            this.form.centerDetailList=this.centerDetailList
            addStudyCenter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studyIds = row.studyId || this.ids;
      this.$modal.confirm('是否确认删除学习中心编号为"' + studyIds + '"的数据项？').then(function() {
        return delStudyCenter(studyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/studyCenter/export', {
        ...this.queryParams
      }, `studyCenter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
